//
//  AWOptionsView.m
//  awu
//
//  Created by centling on 15/8/21.
//  Copyright (c) 2015年 centling. All rights reserved.
//

#import "AWOptionsView.h"
#import "AWOptionButton.h"

@implementation AWOptionsView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


-(void)addButtonWithViewWidth:(float)viewWidth array:(NSArray *)array buttonNumOnLine:(NSInteger)num{
    self.layer.masksToBounds=YES;
    self.buttonMutableArray=[[NSMutableArray alloc] init];
    self.buttonSelectedArray=[[NSMutableArray alloc] init];
    self.rowNum=(int)(([array count]-1)/num)+1;
    self.sumHeight=self.rowNum*(ButtonHeight+VerticalInterverl)-VerticalInterverl;
    
    CGSize size={
        .width=[array count]==1?0:(viewWidth-HorizontalInterverl*num)/num,
        .height=ButtonHeight,
    };
    
    CGPoint point={
        .x=0,
        .y=0,
    };
    for (int i=0; i<self.rowNum; i++) {
        for (int j=0; j<num; j++) {
            if ((i*num+j)==[array count]) {
                break;
            }
            AWOptionButton *button=[[AWOptionButton alloc] initWithFrame:CGRectMake(point.x+j*(size.width+HorizontalInterverl),point.y+i*(ButtonHeight+VerticalInterverl), size.width, size.height)];
            [self.buttonMutableArray addObject:button];
            [self.buttonSelectedArray addObject:@0];
            button.layer.borderWidth=0.5;
            button.titleLabel.font=[UIFont systemFontOfSize:13];
            [self setOptionButtonUnselected:button];
            if ([[array[i*num+j] class] isSubclassOfClass:[NSString class]]) {
                [button setTitle:array[i*num+j] forState:UIControlStateNormal];
            }else if ([array[i*num+j] class]==[UIImage class]){
                [button setImage:array[i*num+j] forState:UIControlStateNormal];
            }
            button.tag=i*num+j;
            [button addTarget:self action:@selector(optionButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:button];
        }
    }
}


-(void) optionButtonClick:(id)sender{
    AWOptionButton *button=(AWOptionButton *)sender;
    if ([self.buttonSelectedArray[button.tag] isEqual:@0]) {
        [self setOptionButtonSelected:button];
    }else{
        [self setOptionButtonUnselected:button];
    };
    if (button==self.buttonMutableArray[self.exclusiveButtonIndex]) {
        [self setOptionButtonSelected:self.buttonMutableArray[self.exclusiveButtonIndex]];
        for (int i=0; i<[self.buttonMutableArray count]; i++) {
            if (i!=self.exclusiveButtonIndex) {
                AWOptionButton *tmpButton=self.buttonMutableArray[i];
                [self setOptionButtonUnselected:tmpButton];
            }
        }
    }else{
        [self setOptionButtonUnselected:self.buttonMutableArray[self.exclusiveButtonIndex]];
    }
}

-(void) setOptionButtonSelected:(AWOptionButton *)button{
    button.layer.borderColor=[UIColor colorWithRed:32/255.0 green:164/255.0  blue:194/255.0  alpha:1 ]
.CGColor;
    button.selectedImage.hidden=NO;
    [button setTitleColor:[UIColor colorWithRed:32/255.0 green:164/255.0  blue:194/255.0  alpha:1 ]
 forState:UIControlStateNormal];
    self.buttonSelectedArray[button.tag]=@1;
}

-(void) setOptionButtonUnselected:(AWOptionButton *)button{
    button.layer.borderColor=[UIColor grayColor].CGColor;
    button.selectedImage.hidden=YES;
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.buttonSelectedArray[button.tag]=@0;
}


-(void)setAllButtonsDisable{
    for (AWOptionButton *button in self.buttonMutableArray) {
        button.userInteractionEnabled=NO;
    }
}

-(void)resetAllButtons{
    for (AWOptionButton *button in self.buttonMutableArray) {
        [self setOptionButtonUnselected:button];
    }
}
                              
@end
